#include "dgt_IfControl.h"
#include <wx/image.h>
#include <wx/dc.h>
#include <wx/pen.h>
#include <wx/dcbuffer.h>

BEGIN_EVENT_TABLE (dgt_IfControl, wxControl)
EVT_LEFT_DOWN (dgt_IfControl::OnLeftDown)
EVT_MOTION (dgt_IfControl::OnMove)
EVT_LEFT_UP (dgt_IfControl::OnLeftUp)
EVT_PAINT (dgt_IfControl::OnPaint)
EVT_LEFT_DCLICK (dgt_IfControl::OnDClick)
EVT_CONTEXT_MENU (dgt_IfControl::OnContextMenu)
EVT_MENU (wxID_DELETE, dgt_IfControl::OnDeleteClick)
EVT_ERASE_BACKGROUND(dgt_IfControl::OnEraseBackground)
EVT_ENTER_WINDOW(dgt_IfControl::OnEnter)
EVT_LEAVE_WINDOW(dgt_IfControl::OnLeave)

END_EVENT_TABLE ()

dgt_IfControl::dgt_IfControl(void)
{
}

dgt_IfControl::~dgt_IfControl(void)
{
}

dgt_IfControl::dgt_IfControl(wxWindow* parent, wxWindowID id, 
										   const wxPoint& pos, const wxSize& size, long style,
										   const wxValidator& validator
										   )
										   : wxControl(parent, id, pos, size, style , validator)
										   , m_dragF( false ), m_drag(false), m_ldown(false), m_enter(false), m_top_marker(false),
										   m_bottom_marker(false)



{
}	
//Painting control
void dgt_IfControl::OnPaint(wxPaintEvent& event)
{

	wxPoint pt[4];
	/*pt[0].x= GetSize().GetX()/2;
	pt[0].y = 0;
	pt[1].x= 0;
	pt[1].y = GetSize().GetX()/2;
	pt[2].x= GetSize().GetX();
	pt[2].y = GetSize().GetX()/2;
	pt[3].x= GetSize().GetX()/2;
	pt[3].y = GetSize().GetX();*/
	
	Refresh();
	wxPaintDC dc(this);
	dc.SetBrush(*wxWHITE_BRUSH);
	dc.SetPen (*wxWHITE_PEN);
	dc.DrawRectangle( 0, 0,  GetSize().GetX(), GetSize().GetY() );
	SetBackgroundColour(*wxWHITE);
	dc.SetBrush( *wxWHITE_BRUSH);
	wxPen *pen = new wxPen (*wxBLACK, 1);
	dc.SetPen(*pen);
	/*dc.DrawLine(GetSize().GetX()/2,0,0,GetSize().GetY()/2);
	dc.DrawLine(GetSize().GetX()/2,0,GetSize().GetY(),GetSize().GetY()/2 );
	dc.DrawLine(GetSize().GetX(),GetSize().GetY()/2,GetSize().GetY()/2,GetSize().GetY());
	dc.DrawLine(0,GetSize().GetY()/2,GetSize().GetY()/2,GetSize().GetY());*/
	if (m_drag||m_enter)
	{
		wxPen *pen1 = new wxPen (*wxBLACK, 1, wxDOT);
		dc.SetPen(*pen1);
		//dc.SetBrush( wxColour( 213, 212, 199 ) );
		dc.DrawLine(GetSize().GetX()/2,0,0,GetSize().GetY()/2);
		dc.DrawLine(GetSize().GetX()/2,0,GetSize().GetX(),GetSize().GetY()/2 );
		dc.DrawLine(GetSize().GetX()/2,GetSize().GetY(),0,GetSize().GetY()/2);
		dc.DrawLine(GetSize().GetX()/2,GetSize().GetY(),GetSize().GetX(),GetSize().GetY()/2);

	} else 
	{
		dc.SetBrush(*wxWHITE_BRUSH);
		dc.SetPen (*wxWHITE_PEN);
		dc.DrawRectangle( 0, 0,  GetSize().GetX(), GetSize().GetY() );
		SetBackgroundColour(*wxWHITE);
		dc.SetBrush( *wxWHITE_BRUSH);
		wxPen *pen = new wxPen (*wxBLACK, 1);
		dc.SetPen(*pen);
		dc.DrawLine(GetSize().GetX()/2,0,0,GetSize().GetY()/2);
		dc.DrawLine(GetSize().GetX()/2,0,GetSize().GetX(),GetSize().GetY()/2 );
		dc.DrawLine(GetSize().GetX()/2,GetSize().GetY(),0,GetSize().GetY()/2);
		dc.DrawLine(GetSize().GetX()/2,GetSize().GetY(),GetSize().GetX(),GetSize().GetY()/2);
	}
	/*if (m_top_marker) 
	{
		
		
		dc.SetBrush(*wxWHITE_BRUSH);
		dc.DrawRectangle( 0, 0, 100, 100 );
		wxPen *pen1 = new wxPen (*wxBLACK, 3);
		dc.SetPen(*pen1);
		dc.DrawLine(GetSize().GetX()/2,0,0,GetSize().GetY()/2);
		dc.DrawLine(GetSize().GetX()/2,0,GetSize().GetY(),GetSize().GetY()/2 );
		dc.DrawLine(GetSize().GetX(),GetSize().GetY()/2,GetSize().GetY()/2,GetSize().GetY());
		dc.DrawLine(0,GetSize().GetY()/2,GetSize().GetY()/2,GetSize().GetY());
		dc.SetBrush(*wxBLACK_BRUSH);

		dc.DrawRectangle(45,90, 10,10);
	}
	if (m_bottom_marker)
	{

		dc.SetBrush(*wxWHITE_BRUSH);
		dc.DrawRectangle( 0, 0, 100, 100 );
		wxPen *pen1 = new wxPen (*wxBLACK, 3);
		dc.SetPen(*pen1);
		dc.DrawLine(GetSize().GetX()/2,0,0,GetSize().GetY()/2);
		dc.DrawLine(GetSize().GetX()/2,0,GetSize().GetY(),GetSize().GetY()/2 );
		dc.DrawLine(GetSize().GetX(),GetSize().GetY()/2,GetSize().GetY()/2,GetSize().GetY());
		dc.DrawLine(0,GetSize().GetY()/2,GetSize().GetY()/2,GetSize().GetY());
		dc.SetBrush(*wxBLACK_BRUSH);
		dc.DrawRectangle(45,0, 10,10);
	}*/
	/*dc.DrawRectangle(75,140, 10,10);
	dc.DrawRectangle(75,0, 10,10);*/
}


	
	




/***********************************************************************************************/
wxSize dgt_IfControl::DoGetBestSize() const
{

	wxSize best;


	best.x = 100;
	best.y = 100;


	return best;


}

/*****************************************************************/
//Adding text to the control
void dgt_IfControl::AddText(wxString  string)
{

	Refresh();
	wxStaticText * statText;
	this->Refresh(true,NULL);
	statText = new wxStaticText(this, wxID_ANY, string, wxPoint(30,30),
		wxDefaultSize, wxALIGN_RIGHT , wxEmptyString);
	statText->SetFont(wxFont(12, wxSWISS, wxNORMAL, wxBOLD, false, wxT("Tahoma")));

}

/************************************************************************/
//On mouse double click event appears adding text dialog
void dgt_IfControl::OnDClick(wxMouseEvent& event)
{
	Refresh();
	this->DestroyChildren();
	wxTextEntryDialog * TextDlg = new wxTextEntryDialog(this, _T("Please, enter text"), _T("Text dialog"),
		_T(""), wxOK | wxCANCEL | wxCENTRE, wxDefaultPosition);
	TextDlg->ShowModal();
	wxString string;
	string = TextDlg->GetValue();
	AddText(string);
}
/*******************************************************/
//On mouse left button down event
void dgt_IfControl::OnLeftDown(wxMouseEvent &event )
{

	Refresh();
	m_ldown = true;
	CaptureMouse(); 
	wxPoint pos = ClientToScreen(event.GetPosition()); 
	wxPoint origin = GetPosition(); 
	int dx =  pos.x - origin.x; 
	int dy = pos.y - origin.y;
	m_Point = wxPoint(dx, dy);

}
/****************************************/
//It's a kind of moving control
void dgt_IfControl::OnMove(wxMouseEvent &event)
{
	wxPoint pt = event.GetPosition(); 
	wxPoint pos = ClientToScreen(pt); 

	if (event.Dragging() && event.LeftIsDown()) 
	{ 
		m_drag = true;
		//wxPoint pos = ClientToScreen(pt); 
		Move(wxPoint(pos.x - m_Point.x, pos.y - m_Point.y)); 
	}

	if ((pt.x>=50)&&(pt.x<=83)&&(pt.y>=40)&&(pt.y<=100)) m_top_marker = true;
	else if ((pt.x>=50)&&(pt.x<=83)&&(pt.y<=20)) m_bottom_marker = true;
	else { m_bottom_marker = false; m_top_marker = false;}
}

/***************************************************/
////On mouse left button up event
void dgt_IfControl::OnLeftUp(wxMouseEvent & event)
{
	Refresh();
	m_drag = false;
	m_ldown = false;
	if (HasCapture()) 
	{ 
		ReleaseMouse(); 
	}
}
/************************************************************/
//appending context menu when right button is pressed on the control
void dgt_IfControl::OnContextMenu(wxContextMenuEvent & event)
{
Refresh();
	wxPoint point = event.GetPosition();
	if (point.x == -1 && point.y == -1) {
		wxSize size = GetSize();
		point.x = size.x / 2;
		point.y = size.y / 2;
	} 
	else 
	{
		point = ScreenToClient(point);
	}
	wxMenu menu;

	menu.Append(wxID_DELETE, _T("&Delete"));
	menu.AppendSeparator();
	menu.Append(wxID_CANCEL, _T("Cancel"));

	this->PopupMenu(&menu, point.x, point.y);

}
/***********************************************************/
//deleting control
void dgt_IfControl::OnDeleteClick(wxCommandEvent &event)
{
	this->Destroy();
}
/****************************************************************/
//I suppose it avoids flickering but it doesn't
void dgt_IfControl::OnEraseBackground(wxEraseEvent &WXUNUSED(event)) 
{ 
	
	//don't do anything here 
} 
/*********************************************************/
//Checks if mouse enters control and changes flag if it does
void dgt_IfControl::OnEnter( wxMouseEvent &event )
{

	m_enter = true;
	Refresh();
}

/**********************************************************************************************/
//Checks if mouse leaves control and changes flag if it does
void dgt_IfControl::OnLeave( wxMouseEvent &event )
{

	m_enter = false;

	Refresh();
}
/*******************************************************************/

/**************************************************/
//checks if control is enable and changes flags if it isn't
void dgt_IfControl::SetEnable( bool c_enable )
{
	m_enableF = c_enable;

	if( !m_enableF )
	{
		m_dragF = false;

	}

	Refresh();
}
/**************************************************/
